Ví dụ Biến_thành_viên

C++

 1 #include <iostream> 2 class Foo { 3     int bar; // Member variable 4   public: 5     void setBar (const int newBar) { bar = newBar; } 6 }; 7  8 int main () { 9   Foo rect; // Local variable10 11   return 0;12 }

Java

class Program{    static void main(final String arguments[])    {    	// This is a local variable. Its lifespan    	// is determined by lexical scope.    	Foo foo;    }}class Foo{    // This is a member variable - a new instance    // of this variable will be created for each     // new instance of Foo.  The lifespan of this    // variable is equal to the lifespan of "this"    // instance of Foo    int bar;}